package main

import (
	"database/sql"
	"fmt"
	"ginweb02email/common"
	_ "github.com/go-sql-driver/mysql" // 导入 MySQL 驱动
	"log"
)

func main11() {
	//password := "111111aA"

	fmt.Println(common.InitMd5("111111aA"))
	md5 := "ffb9355e47c90c2e62c8bbd0082aec68"
	pwd1 := common.PasswordHash(md5)
	fmt.Println(pwd1)
	flag := common.PasswordVerify("$2a$04$lZ9M37OTye6w9rPP0Caq2OvHWDxxL.APaLg/8s5DHR4lxsxbYaPqS", md5)
	fmt.Println("密码比对结果：", flag)
}

func main111() {
	dbUser := "root"
	dbPass := "database_Sugon123"
	dbHost := "10.2.106.54:3306"
	dbName := "dasapp_commhub"

	// 构建连接字符串
	dataSource := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True",
		dbUser, dbPass, dbHost, dbName)

	// 打开数据库连接
	db, err := sql.Open("mysql", dataSource)
	if err != nil {
		log.Fatal("连接数据库失败:", err)
	} else {
		fmt.Println("连接数据库成功")
	}
	defer db.Close() // 确保程序退出时关闭连接

	// 查询用户
	querySQL := "SELECT id, old_passwd FROM ums_db.tb_user_member WHERE old_passwd IS NOT NULL"
	rows, err := db.Query(querySQL)
	if err != nil {
		log.Fatal("查询失败:", err)
	}
	defer rows.Close()

	// 遍历查询结果
	for rows.Next() {
		var id int
		var oldPassword string
		err = rows.Scan(&id, &oldPassword)
		if err != nil {
			log.Fatal("扫描行失败:", err)
		}

		// 加密密码
		hashedPassword := common.PasswordHash(oldPassword)

		// 更新数据库，将加密后的密码存储到 password 列
		updateSQL := "UPDATE ums_db.tb_user_member SET passwd = ? WHERE id = ?"
		_, err = db.Exec(updateSQL, string(hashedPassword), id)
		if err != nil {
			log.Fatal("更新失败:", err)
		}
		fmt.Printf("用户 ID=%d 的密码已加密并更新\n", id)
	}

}

func main3() {
	// 示例数据
	//password := "111111aA"
	md5 := "5e14bd80a2e46391d29c18cea3255260"
	pwd1 := common.PasswordHash(md5)
	fmt.Println("加密后的密码：", pwd1)
	flag := common.PasswordVerify("$2a$04$1BHs4ZEM2no/jQzedrZKPuXO4/Si6y94zZJTvEDtVNjR.XsFi9hL2", md5)
	fmt.Println("密码比对结果：", flag)
}
